package com.wuji.salary.common;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiAttendanceGetsimplegroupsRequest;
import com.dingtalk.api.request.OapiAttendanceGroupMemberusersListRequest;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiAttendanceGetsimplegroupsResponse;
import com.dingtalk.api.response.OapiAttendanceGroupMemberusersListResponse;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.taobao.api.ApiException;
import org.springframework.context.annotation.ComponentScan;
import shade.com.alibaba.fastjson2.JSONArray;
import shade.com.alibaba.fastjson2.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.wuji.salary.common.GetToken.token;

/**
 * 获取所有考勤人员的 userId
 */
@ComponentScan
public class GetAllUserId {

    /**
     * 根据考勤组 groupid 去找到所有参与考勤人员的userId
     */
    public static List<List<String>> getAllUserId(){
        List<List<String>> allResults = new ArrayList<>();
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/group/memberusers/list");
            OapiAttendanceGroupMemberusersListRequest req = new OapiAttendanceGroupMemberusersListRequest();
//            req.setOpUserId("17211808184024666");
            req.setOpUserId("331004400521732855");
            ArrayList<Integer> groupIdList = getGroupId();
            for (Integer groupId : groupIdList){
                String str = groupId + "L";
                str = str.substring(0, str.length() - 1);
                Long num = Long.valueOf(str);
                req.setGroupId(num);
                OapiAttendanceGroupMemberusersListResponse rsp = client.execute(req, token);
                System.out.println(rsp.getBody());
                List<String> result = rsp.getResult().getResult();
                System.out.println(result);
                allResults.add(result);
            }
            return allResults;
        } catch (ApiException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取考勤组 id 是个集合
     */
    public static ArrayList<Integer> getGroupId(){
        ArrayList<Integer> integers = new ArrayList<>();
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getsimplegroups");
            OapiAttendanceGetsimplegroupsRequest req = new OapiAttendanceGetsimplegroupsRequest();
            OapiAttendanceGetsimplegroupsResponse rsp = client.execute(req, token);
            System.out.println(rsp.getBody());

            JSONObject jsonObject = JSONObject.parseObject(rsp.getBody());
            JSONObject result = jsonObject.getJSONObject("result");
            JSONArray groups = result.getJSONArray("groups");
            for (Object group : groups) {
                JSONObject groupObject = (JSONObject) group;
                int groupId = groupObject.getIntValue("group_id");
                integers.add(groupId);
                System.out.println(integers);
            }
            return integers;
        } catch (ApiException e) {
            e.printStackTrace();
            return null;
        }
    }
}
